/**
 * Ermittelt die Postion der oberen linken Ecke des gegebenen Elements auf der X-Achse
 */
function /* : number : */ getElementPosX(/* : Object : */ object)
{
	var curleft = 0;
	if (object.offsetParent)
	{
		while (object.offsetParent)
		{
			curleft += object.offsetLeft
			object = object.offsetParent;
		}
	}
	else if (object.x)
		curleft += object.x;
	return curleft;
}

/**
 * Ermittelt die Postion der oberen linken Ecke des gegebenen Elements auf der Y-Achse
 */
function /* : number : */ getElementPosY(/* : Object : */ object)
{
	var curtop = 0;
	if (object.offsetParent)
	{
		while (object.offsetParent)
		{
			curtop += object.offsetTop
			object = object.offsetParent;
		}
	}
	else if (object.y)
		curtop += object.y;
	return curtop;
}


var lastevent;

function saveKeyPressEvent(e) {
	lastevent = e;

	var code = getKeyCode(e);
	if (code == 27) {
  	closeSelect();
  }
}

function pressedCtrlSpace() {
  var code = getKeyCode(lastevent);
	var event = getCurrentEvent();
  return (event.ctrlKey && code == 32);
}

function getCurrentEvent() {
	var event = lastevent;
	if (!event) {
		event = window.event;
	}
	return event;	
}

function getKeyCode(event) {
	if (!event) {
		event = window.event;
	}

	try {
		if (event.which) {
			return event.which;
		}
	} catch(ex) {
	}	

	try {
		if (event.keyCode) {
			return event.keyCode;
		}
	} catch(ex) {
	}	

	return null;
}

var currentReference;

function assistACodes(input, classId) {
	if (pressedCtrlSpace()) {
		currentReference = input;
		getACodes(classId);
	}
}

function getACodes(classId) {
  TextModuleUtil.getAvailableACodes(classId, currentReference.name, selectACode);
}

function assistOptions(input, aCode) {
	if (pressedCtrlSpace()) {
		currentReference = input;
		getOptions(aCode);
	}
}

function getOptions(aCode) {
  TextModuleUtil.getOptions(aCode, selectText);
}

function assistSpecACodes(input, codes) {
	if (pressedCtrlSpace()) {
		currentReference = input;
		getSpecACodes(codes);
	}
}

function getSpecACodes(codes) {
  TextModuleUtil.getACodeLabels(codes, selectACode);
}



function selectACode(data) {

	var select = getSelect();
	DWRUtil.removeAllOptions(select.id);
	DWRUtil.addOptions(select.id, data, "value", "label");

	initSelect(select, data.length, setInputAcode);
}

function selectText(data) {
	var select = getSelect();
	DWRUtil.removeAllOptions(select.id);
	DWRUtil.addOptions(select.id, data, "value", "label");

	initSelect(select, data.length, setInputText);
}

function setInputAcode(e) {
  var code = getKeyCode(e);
  if (code == 13) {
  	getOptions(DWRUtil.getValue(this.id));
  }
}

function setInputText(e) {
	var code = getKeyCode(e);

  if (code == 13) {
  	doAppend(DWRUtil.getValue(this.id));
		closeSelect();
  }
}

function doAppend(text) {
	currentReference.value += text + " ";
}


function getSelect() {
	var sel = document.getElementById("assist");

	if (!sel) {
		sel = document.createElement("select");
		document.body.appendChild(sel);

		sel.id = "assist";
		sel.style.position = "absolute";
	}

	sel.focus();
	return sel;
}

function closeSelect() {
	var select = document.getElementById("assist");

	if (select) {
		document.body.removeChild(select);
		if (currentReference) {
			currentReference.focus();
			currentReference = null;
		}
	}
}

function initSelect(select, dataLength, keyPressedFunc) {
	select.style.top = getElementPosY(currentReference) + 20;
	select.style.left = getElementPosX(currentReference);

	if (dataLength >= 7) {
		select.size = 7;
	} else {
		select.size = dataLength;
	}
	
	if (dataLength > 0) {
		select.options[0].selected = true;
	}

	if (keyPressedFunc) {
		select.onkeypress = keyPressedFunc;
	}
}

// all objects

document.onkeypress = saveKeyPressEvent;

//function addEventListenersToTags(tagName) {
//	
//}